﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Studio.Custom;
using DWF.Studio.Share.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace DWF.Studio.Project
{
    /// <summary>
    /// 工程文件详情
    /// </summary>
    internal class ProjectDetail
    {
        public ProjectDetail()
        {
            dependency = new List<string>();
            file = new List<string>();
            libs = new List<string>();
            script = new List<string>();
            images = new List<string>();
            res = new List<string>();
        }

        /// <summary>
        /// 工程id
        /// </summary>
        public string id
        {
            set;
            get;
        }

        /// <summary>
        /// 名称
        /// </summary>
        public string name
        {
            set;
            get;
        }

        /// <summary>
        /// 描述
        /// </summary>
        public string description
        {
            set;
            get;
        }

        /// <summary>
        /// 文档规范版本
        /// </summary>
        public string ver
        {
            set;
            get;
        }

        /// <summary>
        /// 工程修改版本号
        /// </summary>
        public string reversion
        {
            set;
            get;
        }

        /// <summary>
        /// 目标平台
        /// </summary>
        public string platform
        {
            set;
            get;
        }

        /// <summary>
        /// 工作流语言
        /// </summary>
        public string wflanguage
        {
            set;
            get;
        }

        /// <summary>
        /// 启动项
        /// </summary>
        public string start
        {
            set;
            get;
        }

        /// <summary>
        /// 依赖活动包
        /// </summary>
        public List<string> dependency
        {
            set;
            get;
        }

        /// <summary>
        /// xaml文件夹
        /// </summary>
        public List<string> file
        {
            set;
            get;
        }

        /// <summary>
        /// 库文件夹
        /// </summary>
        public List<string> libs
        {
            set;
            get;
        }

        /// <summary>
        /// 脚本文件夹
        /// </summary>
        public List<string> script
        {
            set;
            get;
        }

        /// <summary>
        /// 图像文件夹
        /// </summary>
        public List<string> images
        {
            set;
            get;
        }

        /// <summary>
        /// 资源文件夹
        /// </summary>
        public List<string> res
        {
            set;
            get;
        }

        /// <summary>
        ///  工程文件详情转换为工程信息
        /// </summary>
        /// <param name="detail"></param>
        /// <param name="projectPath"></param>
        /// <returns></returns>
        public ProjectInfo ConvertToProjectInfo(string projectPath)
        {
            ProjectInfo proInfo = new ProjectInfo()
            {
                Id = this.id ?? Guid.NewGuid().ToString("N"),
                Name = this.name ?? Path.GetFileNameWithoutExtension(projectPath),
                Description = this.description,
                DisplayName = this.name ?? Path.GetFileNameWithoutExtension(projectPath),
                FullName = projectPath,
                Version = this.ver,
                TargetPlatform = this.platform,
                WFLanguage = this.wflanguage,
                ProjectVersion = this.reversion,
                IsExisting = FileHelper.CheckFolderExisting(projectPath)
            };

            convertSelfDependOn(proInfo, StaticResource.ProjectSelfDependOn, this.dependency);
            convertSelfFolder(proInfo, this.start, StaticResource.ProjectSelfFile, this.file);
            convertSelfFolder(proInfo, String.Empty, StaticResource.ProjectSelfLibs, this.libs);
            convertSelfFolder(proInfo, String.Empty, StaticResource.ProjectSelfImages, this.images);
            convertSelfFolder(proInfo, String.Empty, StaticResource.ProjectSelfRes, this.res);

            return proInfo;
        }

        /// <summary>
        /// 转换相关文件
        /// </summary>
        /// <param name="proInfo"></param>
        /// <param name="start"></param>
        /// <param name="folderName"></param>
        /// <param name="files"></param>
        private void convertSelfFolder(ProjectInfo proInfo, string start, string folderName, List<string> files)
        {
            ProjectFolder folder = new ProjectFolder(true)
            {
                DisplayName = folderName,
                Name = folderName,
                FullName = Path.Combine(proInfo.FullName, folderName),
                Parent = proInfo,
                IsExisting = FileHelper.CheckFolderExisting(Path.Combine(proInfo.FullName, folderName))
            };

            foreach (var filename in files)
            {
                ProjectFile file = null;
                if (start == filename)
                {
                    file = new ProjectFile(true);
                    proInfo.StartFile = file;
                    file.IsStart = true;
                }
                else
                {
                    file = new ProjectFile();
                }

                file.Name = filename;
                file.DisplayName = filename;
                file.FullName = Path.Combine(proInfo.FullName, folderName, filename);
                file.Parent = folder;
                file.IsExisting = FileHelper.CheckFileExisting(file.FullName);
                folder.Children.Add(file);
            }

            proInfo.Children.Add(folder);
        }

        /// <summary>
        /// 转换相关依赖
        /// </summary>
        /// <param name="proInfo"></param>
        /// <param name="folderName"></param>
        /// <param name="items"></param>
        private void convertSelfDependOn(ProjectInfo proInfo, string folderName, List<string> items)
        {
            ProjectDependencies dependOnItems = new ProjectDependencies()//工程依赖节点
            {
                DisplayName = folderName,
                Name = folderName,
                FullName = Path.Combine(proInfo.FullName, folderName),
                Parent = proInfo,
                IsExisting = true
            };

            foreach (var item in items)
            {
                Regex reg = new Regex("id=(.+);version=(.+);targetFramework=(.+)");//依赖信息
                Match match = reg.Match(item);
                string id = match.Groups[1].Value;
                string version = match.Groups[2].Value;
                string targetFramework = match.Groups[3].Value;

                ProjectPackage dependOnItem = new ProjectPackage();
                dependOnItem.Id = id;
                dependOnItem.Version = version;
                dependOnItem.TargetFramework = targetFramework;
                dependOnItem.Name = id; ;
                dependOnItem.DisplayName = $"{id}-{version}";
                dependOnItem.FullName = item;
                dependOnItem.Parent = dependOnItems;
                var baseDirectory = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory).Parent.FullName;
                var dependOnPath = Path.Combine(baseDirectory, StaticResource.Activities, String.Format(@"{0}.{1}", id, version), StaticResource.ActivitiesLib, targetFramework);
                dependOnItem.IsExisting = FileHelper.CheckFolderExisting(dependOnPath);

                dependOnItems.Children.Add(dependOnItem);
            }

            proInfo.Children.Add(dependOnItems);
        }
    }
}
